package com.zlzxm.studentscreen.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.mmkv.MMKV;
import com.zlzxm.studentscreen.R;
import com.zlzxm.studentscreen.globe.Constant;
import com.zlzxm.studentscreen.net.http.RetrofitClient;
import com.zlzxm.studentscreen.net.http.repository.MyRepository;
import com.zlzxm.studentscreen.service.ServiceStart;
import com.zlzxm.studentscreen.ui.activity.manager.ActivityStarter;
import com.zlzxm.studentscreen.util.ToastyUtil;
import com.zlzxm.studentscreen.util.ZViewHelp;

import io.reactivex.functions.Consumer;

/**
 * CREATE BY zlzxm ON 2022/3/9
 */
public class AcStart extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_test);
        requestPermission();
//        test();
    }

    private void test(){

        TextView textView = findViewById(R.id.tvContent);

        String content = textView.getText().toString();
        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
//        dm.density = 3;
//        dm.densityDpi = 480;
        float density  = dm.density;        // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
        int densityDPI = dm.densityDpi;     // 屏幕密度（每寸像素：120/160/240/320）
        float xdpi = dm.xdpi;
        float ydpi = dm.ydpi;

        int screenWidth  = dm.widthPixels;      // 屏幕宽（像素，如：480px）
        int  screenHeight = dm.heightPixels;     // 屏幕高（像素，如：800px）
        content = content+"\n"+ "density:"+density+"\n"+"densityDPI:"+densityDPI+"\n"
                + "screenWidth"+ screenWidth+"\n"+"screenHeight:"+screenHeight;
        textView.setText(content);


    }



    private void task(){

        boolean notFirstOpen = MMKV.defaultMMKV().decodeBool(Constant.MMKVkey.NOT_FIRST_OPEN);

        if(!notFirstOpen){

            ActivityStarter.startAcConfig(this);

        }else {

            String server =   MMKV.defaultMMKV().decodeString(Constant.MMKVkey.SERVER);
            RetrofitClient.reSet(server);

            String webSocket =   MMKV.defaultMMKV().decodeString(Constant.MMKVkey.WEBSOCKET);
            if(!TextUtils.isEmpty(webSocket)){

                ServiceStart.startCoreService(AcStart.this,webSocket);
            }
            ActivityStarter.startAcHome(this);
        }

    }



    private void requestPermission(){

        RxPermissions rxPermissions = new RxPermissions(this);

        rxPermissions.request(Constant.Permissions.WIFI_SSID)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {

                        if(aBoolean){
                            task();
                        }else {
                            ToastyUtil.errorToast(AcStart.this,"缺少权限，退出app");
                            finish();
                        }
                    }
                });

    }
}
